layui.define(['form','table'], function(exports){
  var $ = layui.$,form = layui.form,table = layui.table;
  var deviceSn = $('input[name="deviceSn"]').val();

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/cdcz/device/slot/list.data'
      ,where:{deviceSn:deviceSn}
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
      {field: 'slotNum', width:80, title: '卡槽号'}
      ,{field: 'overloadStateName', width:80,title: '过载'}
      ,{field: 'temperatureStateName', width:80,title: '温度'}
      ,{field: 'stateName', width:80,title: '状态'}
      ,{field: 'currentTradeNo', title: '最近订单'}
      ,{field: 'updateDate', title: '最后修改时间'}
      ,{title: '操作', width: 160, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
    ]]
    ,page: false
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });

  //事件
  var active = {
      search:function(){
        layer.confirm('确定要发送查询命令吗？', function(index){
            $.ajax({
                type: "post",
                url: layui.setter.webRoot + '/cdcz/device/operate/search.do',
                data:{deviceSn:deviceSn},
                dataType:"json",
                success:function(res){
                    if(res.code == 200){
                        layer.msg('查询成功',{icon: 1},function (){
                            table.reload('LAY-table'); //数据刷新
                        },5000);
                    }else{
                        layer.msg(res.msg,{icon: 5});
                    }
                }
            });
            layer.close(index);
        });
    }
    ,restart:function(){
          layer.confirm('确定要重启设备吗？', function(index){
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/cdcz/device/operate/restart.do',
                  data:{deviceSn:deviceSn},
                  dataType:"json",
                  success:function(res){
                      if(res.code == 200){
                          layer.msg('设备重启成功',{icon: 1},function (){
                              table.reload('LAY-table'); //数据刷新
                          },5000);
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
      }
    ,charge: function(obj){
      layer.open({
        type: 2
        ,title: '测试充电'
        ,content: layui.setter.webRoot + '/cdcz/device/operate/slot/charge.html?deviceSn='+obj.data.deviceSn + '&slotNum='+obj.data.slotNum
        ,maxmin: true
        ,area: ['500px', '450px']
        ,btn: ['确定', '取消']
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
          ,submitID = 'LAY-form-submit',submit = layero.find('iframe').contents().find('#'+ submitID);
          iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
            var field = data.field;
        	$.ajax({
	    		type: "post",
	            url: layui.setter.webRoot + '/cdcz/device/operate/slot/charge.do',
	            data:field,
				dataType:"json",
				success:function(res){
					if(res.code == 200){
						table.reload('LAY-table'); //数据刷新
						layer.close(index); //关闭弹层
					}else{
						layer.msg(res.msg,{icon: 2});
					}
				}
	        });
          });  
          submit.trigger('click');
        }
      }); 
    }
    ,closeCharge:function(obj){
    	layer.confirm('确定要发送关闭充电命令吗？', function(index){
	    	$.ajax({
	      	  type: "post",
	          url: layui.setter.webRoot + '/cdcz/device/operate/slot/charge/close.do',
	          data:{deviceSn:obj.data.deviceSn,slotNum:obj.data.slotNum},
	  		  dataType:"json",
	  		  success:function(res){
	  			if(res.code == 200){
                    table.reload('LAY-table'); //数据刷新
	  			}else{
	  				layer.msg(res.msg,{icon: 5});
	  			}
	  		  }
	        });
	    	layer.close(index);
	    });
    }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('charge_socket_info', {})
});